package com.fw.kafka_chapter_1.quick;

import com.fw.kafka_chapter_1.quick.interceptor.ProducerInterceptorShould;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * @Author:yanwei
 * @Date: 2020/8/16 - 17:48
 *
 * Kafka 生产者
 *  广播方式
 */
public class ProducerStart {
    private static final Logger log = LoggerFactory.getLogger(ProducerStart.class);

    public static void main(String[] args) {
        Properties properties = new Properties();
        // key 序列化器指定
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        // value 序列化执行
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());
        // 指定集群地址
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.227.129:9092");
        // 设置重试次数
        properties.put(ProducerConfig.RETRIES_CONFIG,10);
        // 新增拦截器
        properties.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, ProducerInterceptorShould.class.getName());
        //幂等性建设
        properties.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG,"true");
        //// 当 enable.idempotence 为 true，这里默认为 all
        properties.put(ProducerConfig.ACKS_CONFIG,"all");
         // 创建消息发布者
        KafkaProducer<String,String> producer = new KafkaProducer<String, String>(properties);
        // 根据topic 发送消息
        ProducerRecord<String,String> record = new ProducerRecord<>("fw","key","hello_fw_soft");
        try {
            // 发送
            producer.send(record);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            producer.close();
        }
        log.error(StringSerializer.class.getName());
    }


}
